package com.jgy.controller;

import com.jgy.annotation.SetLogValue;
import com.jgy.entity.XmcData;
import com.jgy.entity.vo.LoginInfoVO;
import com.jgy.service.AccountService;
import com.jgy.service.XmcDataService;
import com.jgy.utils.CommonConstants;
import com.jgy.utils.MapperUtils;
import com.jgy.utils.SecurityUtil;
import com.jgy.entity.SpaceUser;
import com.jgy.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;

/**
 * @author 姜光远
 * @date 2023/2/24 9:57
 */
@Slf4j
@RestController
public class TestController {

    @Autowired
    private XmcDataService xmcDataService;
    @Resource
    private AccountService accountService;
    /**
     * spring security oauth2.0 to login
     */
    //@SetLogValue(modular = "登录接口",type = CommonConstants.SELECT, describe = "登录接口")
    @PostMapping(value = "/login")
    public R login(@RequestBody LoginInfoVO loginInfoVO) throws Exception {
        return accountService.login(loginInfoVO);
    }

    @SetLogValue(modular = "测试接口test",type = CommonConstants.SELECT, describe = "测试接口test")
    @RequestMapping("/test")
    public Object loginSuccess() {
        HashMap map = new HashMap();
        map.put(new Date(), LocalDateTime.now());
        SpaceUser user = SecurityUtil.getUser();
        map.put("user",user);
        return R.ok(map);
    }

    @SetLogValue(modular = "测试接口aaa",type = CommonConstants.SELECT, describe = "测试接口aaa")
    @RequestMapping("/aaa")
    public Object loginSuccessaa() throws Exception {
        HashMap map = new HashMap();
        String principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
        SpaceUser spaceUser = MapperUtils.json2pojo(principal, SpaceUser.class);
        System.out.println(spaceUser);
        map.put("aaaaa", LocalDateTime.now());
        return R.ok(map);
    }

    @SetLogValue(modular = "测试接口getList",type = CommonConstants.SELECT, describe = "测试接口getList")
    @PostMapping("/getList")
    public Object getList(String tel){
        XmcData xmcData = xmcDataService.selectList(tel);
        return R.ok(xmcData);
    }

    @SetLogValue(modular = "publish",type = CommonConstants.SELECT, describe = "publish")
    @PostMapping("/publish")
    public Object publish(){

        return R.ok(xmcDataService.publish());
    }

}
